/*
 * Copyright (c) 2022 Huawei Technologies Co.,Ltd.
 *
 * DSS is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * dss_session.h
 *
 *
 * IDENTIFICATION
 *    src/common/dss_session.h
 *
 * -------------------------------------------------------------------------
 */

#ifndef __DSS_SESSION_H__
#define __DSS_SESSION_H__

#include "cm_defs.h"
#include "cs_packet.h"
#include "cs_pipe.h"
#include "dss_defs.h"
#include "cm_thread_pool.h"
#include "dss_protocol.h"
#include "dss_latch.h"
#include "cm_date.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DSS_INVALID_SESSIONID CM_INVALID_ID32
#define DSS_MAX_LATCH_STACK_BOTTON 0
#define DSS_MAX_LATCH_STACK_DEPTH 8
#define DSS_LOCK_CLEAN_SLEEP_TIME 500
#define DSS_BACKGROUND_TASK_NUM 5
#define DSS_SESSION_PAUSED_WAIT 50

typedef struct tagdss_cli_info {
    uint64 cli_pid;
    int64 start_time;
    char process_name[DSS_FILE_NAME_BUFFER_SIZE + 1];
} dss_cli_info;

typedef enum st_dss_latch_offset_type {
    DSS_LATCH_OFFSET_INVALID = 0,
    DSS_LATCH_OFFSET_UNIQ_ID,
    DSS_LATCH_OFFSET_SHMOFFSET,
} dss_latch_offset_type_e;

typedef struct st_dss_latch_offset {
    dss_latch_offset_type_e type;
    union {
        uint32 unique_id;
        uint64 shm_offset;
    } offset;
} dss_latch_offset_t;

typedef struct st_dss_latch_stack {
    dss_latch_offset_t latch_offset_stack[DSS_MAX_LATCH_STACK_DEPTH];
    uint32 stack_top;
} dss_latch_stack_t;

typedef enum en_protocol_type {
    PROTO_TYPE_UNKNOWN = 0,
    PROTO_TYPE_GS = 1,
} protocol_type_t;

typedef enum en_dss_session_status {
    DSS_SESSION_STATUS_IDLE = 0,
    DSS_SESSION_STATUS_RUNNING,
    DSS_SESSION_STATUS_PAUSING,
    DSS_SESSION_STATUS_PAUSED,
} dss_session_status_t;

typedef enum en_dss_wait_event {
    DSS_PREAD = 0,
    DSS_PWRITE,

    DSS_EVT_COUNT,
} dss_wait_event_e;

typedef struct st_dss_session_stat {
    atomic_t total_wait_time;
    atomic_t max_single_time;
    atomic_t wait_count;
} dss_session_stat_t;

typedef struct st_dss_session {
    uint32 id;
    bool32 is_closed;
    bool32 is_used;
    bool32 connected;
    bool32 reactor_added;
    cs_pipe_t pipe;
    dss_packet_t recv_pack;
    dss_packet_t send_pack;
    text_t send_info;  // send extra info, please use recv_pack.init_buf, len = 0 if no info ack
    ftid_t curr_dir;
    protocol_type_t proto_type;  // gauss or mysql (not realized)
    dss_cli_info cli_info;
    dss_latch_stack_t latch_stack;
    bool32 is_direct;
    int32_t log_split;                       // -1 is invalid
    dss_kernel_instance_t *kernel_instance;  // global unique
    volatile uint64 curr_lsn;                // latest lsn generated by current session
    dss_audit_info_t audit_info;
    dss_session_status_t status;
    void *reactor;
    void *workthread_ctx;
    dss_session_stat_t dss_session_stat[DSS_EVT_COUNT];
    uint32 client_version; /* client version */
    uint32 proto_version;  /* client and server negotiated version */
} dss_session_t;

static inline void dss_begin_stat(timeval_t *begin_tv)
{
    (void)cm_gettimeofday(begin_tv);
}

static inline void dss_end_stat(dss_session_t *session, timeval_t *begin_tv, dss_wait_event_e event)
{
    timeval_t end_tv;
    uint64 usecs;

    (void)cm_gettimeofday(&end_tv);
    usecs = (uint64)TIMEVAL_DIFF_US(begin_tv, &end_tv);
    (void)cm_atomic_add(&session->dss_session_stat[event].total_wait_time, (int64)usecs);
    (void)cm_atomic_set(&session->dss_session_stat[event].max_single_time,
        (int64)MAX((uint64)session->dss_session_stat[event].max_single_time, usecs));
    (void)cm_atomic_inc(&session->dss_session_stat[event].wait_count);
}

static inline char *dss_init_sendinfo_buf(char *input)
{
    return (input + sizeof(dss_packet_head_t) + sizeof(int32));
}

typedef struct st_dss_session_ctrl {
    spinlock_t lock;
    bool32 is_inited;
    uint32 used_count;
    uint32 total;
    dss_session_t *sessions;
} dss_session_ctrl_t;

status_t dss_init_session(uint32 max_session_num);
dss_session_ctrl_t *dss_get_session_ctrl(void);
status_t dss_create_session(const cs_pipe_t *pipe, dss_session_t **session);
void dss_destroy_session(dss_session_t *session);

status_t dss_lock_shm_meta_s(dss_session_t *session, const dss_latch_offset_t *offset, latch_t *latch, int32 timeout);
status_t dss_lock_shm_meta_s_without_session(latch_t *latch, bool32 is_force, int32 timeout);
status_t dss_cli_lock_shm_meta_s(
    dss_session_t *session, dss_latch_offset_t *offset, latch_t *latch, latch_should_exit should_exit);
void dss_lock_shm_meta_x(const dss_session_t *session, latch_t *latch);
void dss_lock_shm_meta_x2ix(dss_session_t *session, latch_t *latch);
void dss_lock_shm_meta_ix2x(dss_session_t *session, latch_t *latch);
void dss_unlock_shm_meta(dss_session_t *session, latch_t *latch);
void dss_unlock_shm_meta_without_session(latch_t *latch);
status_t dss_lock_shm_meta_bucket_s(dss_session_t *session, uint32 id, latch_t *latch);
void dss_lock_shm_meta_bucket_x(latch_t *latch);
void dss_unlock_shm_meta_bucket(dss_session_t *session, latch_t *latch);
void dss_clean_session_latch(dss_session_ctrl_t *session_ctrl, dss_session_t *session);
uint32 dss_get_udssession_startid(void);
#ifdef __cplusplus
}
#endif

#endif
